angular.module('app.controllers')
    .controller('ContactCtrl', [
                '$scope', '$ionicScrollDelegate', '$ionicPosition', '$timeout', 'alertService', 'contactService',
        function($scope, $ionicScrollDelegate,    $ionicPosition,    $timeout,   alertService,   contactService) {
            $scope.selected = {
                tag: '',
                show: false
            };
            $scope.userList = [];
            $scope.tags = contactService.tags;
            var mainScroll = $ionicScrollDelegate.$getByHandle('mainScroll');

            contactService.loadAsync().then(function(result) {
                $scope.userList = result;
                $scope.selected.tag = result[0].Tag;
            }, alertService.error);


            $scope.contacts_right_bar_touched = function(event) {
                if ($scope.selected.tag !== event.srcElement.outerText) {
                    $scope.selected.tag = event.srcElement.outerText;
                    $scope.selected.show = true;
                    var selectedElement = document.getElementById($scope.selected.tag);
                    if (selectedElement != null && angular.isDefined(selectedElement)) {
                        var position = $ionicPosition.offset(angular.element(selectedElement));
                        mainScroll.scrollTo(0, mainScroll.getScrollPosition().top + position.top - 150, true);
                    }
                    $timeout(function() {
                        $scope.selected.show = false;
                    }, 500);
                }
            };

            $scope.$watch('searchKey', function() {
                mainScroll.scrollTop();
            });

            $scope.onSelect = function(User) {
                User.Selected = !User.Selected;
                contactService.onSelect(User);
            };

            $scope.ok = function() {
                history.go(-1);
            };
        }]);
        

angular.module('app.services')
    .factory('contactService', [
        '$http', '$q',
        function($http, $q) {
            var service = {};
            var userList = [];
            service.tags = ['☆', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
                            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                             'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

            /**
             * 加载所有联系人
             * @returns {} 
             */
            service.loadAsync = function() {
                var defer = $q.defer();
                if (userList.length <= 0) {
                    var indexes = ['A', 'B', 'C', 'D', 'E', 'F'];
                    for (var i = 0; i < indexes.length; i++) {
                        var temp = { Tag: indexes[i], Users: [] };

                        for (var j = 0; j < 5; j++) {
                            temp.Users.push({
                                UserId: indexes[i] + '00' + j,
                                UserName: indexes[i] + '00' + j,
                                Department: 'Depart' + indexes[i],
                                Selected: false
                            });
                        }
                        userList.push(temp);
                    }
                }
                defer.resolve(userList);

                return defer.promise;
            };
            /**
             * 取消/选择
             * @param {} User 
             * @returns {} 
             */
            service.onSelect = function(User) {
                for (var i = 0; i < userList.length; i++) {
                    for (var j = 0; j < userList[i].Users.length; j++) {
                        var item = userList[i].Users[j];
                        if (User.UserId === item.UserId) {
                            item.Selected = User.Selected;
                            break;
                        }
                    }
                }
            };

            /**
             * 勾选的员工
             * @returns {} 
             */
            service.getSelectedItems = function() {
                var selectedItems = [];
                for (var i = 0; i < userList.length; i++) {
                    for (var j = 0; j < userList[i].Users.length; j++) {
                        var item = userList[i].Users[j];
                        if (item.Selected) {
                            selectedItems.push(item);
                        }
                    }
                }
                return selectedItems;
            };

            /**
             * 清除选择
             * @returns {} 
             */
            service.clearSelection = function() {
                for (var i = 0; i < userList.length; i++) {
                    for (var j = 0; j < userList[i].Users.length; j++) {
                        var item = userList[i].Users[j];
                        if (item.Selected) {
                            item.Selected = false;
                        }
                    }
                }
            };

            return service;
        }]);